﻿using System;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using desarrolloMobile.DGTViewer.ViewModels;
using Microsoft.Phone.Controls;

namespace desarrolloMobile.DGTViewer
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // Constructor
        public DetailsPage()
        {
            InitializeComponent();

            this.Storyboard1.Begin();

            this.BackKeyPress += ((o, s) => Storyboard2.Begin());
            
            this.Loaded += DetailsPage_Loaded;
            
            // la siguiente URL para el inicio en base a ubicacion dispositivo
            //http://dev.virtualearth.net/REST/v1/Locations/47.64054,-122.12934?o=xml&key=Ak5NL1cp_xgLYrjGxl68EfJKGF5jowbb47YMj7f7YoEKgDJkupLHMlEUZJqCEgyT
            // esta para obtener lat y lon de ciudad
            //http://msdn.microsoft.com/en-us/library/ff701714.aspx
            
            //usar 
//            DataContractJsonSerializer x =
//                new DataContractJsonSerializer();
            
        }

        void DetailsPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var autonomia = (DataContext as Incidencia).Autonomia;
            string source = string.Empty;

            switch (autonomia.ToUpper())
            {
                case "ANDALUCIA":
                    source = "Images/Andalucia.jpg";
                    break;
                case "ARAGON":
                    source = "Images/Aragon.jpg";
                    break;
                case "ASTURIAS":
                    source = "Images/Asturias.jpg";
                    break;
                case "COMUNIDAD_CANARIA":
                    source = "Images/Canarias.jpg";
                    break;
                case "CANTABRIA":
                    source = "Images/Cantabria.jpg";
                    break;
                case "CASTILLA-LEON":
                    source = "Images/CastillaLeon.jpg";
                    break;
                case "CASTILLA-LA_MANCHA":
                    source = "Images/CastillaMancha.jpg";
                    break;
                case "CATALUÑA":
                    source = "Images/Catalunya.jpg";
                    break;
                case "CEUTA":
                    source = "Images/Ceuta.jpg";
                    break;
                case "EXTREMADURA":
                    source = "Images/Extremadura.jpg";
                    break;
                case "GALICIA":
                    source = "Images/Galicia.jpg";
                    break;
                case "BALEARES":
                    source = "Images/IslasBaleares.jpg";
                    break;
                case "LA-RIOJA":
                    source = "Images/LaRioja.jpg";
                    break;
                case "MADRID":
                    source = "Images/Madrid.jpg";
                    break;
                case "MELILLA":
                    source = "Images/Melilla.jpg";
                    break;
                case "MURCIA":
                    source = "Images/Murcia.jpg";
                    break;
                case "NAVARRA":
                    source = "Images/Navarra.jpg";
                    break;
                case "PAIS_VASCO":
                    source = "Images/PaisVasco.jpg";
                    break;
                case "VALENCIA":
                    source = "Images/Valencia.jpg";
                    break;
                case "EUSKADI":
                    source = "Images/Euskadi.jpg";
                    break;
                default:
                    source = "";
                    break;
            }

            

            imgAutonomia.Source = 
                new BitmapImage(
                    new Uri(source, UriKind.Relative));

            var tipo = (DataContext as Incidencia).Tipo;
            

            if (tipo.ToUpper() == "RETENCION") source = "Images/Retencion.png";
            else if (tipo.ToUpper() == "OBRAS") source = "Images/Obras.png";
            else if (tipo.ToUpper() == "CONO") source = "Images/Cono.png";
            else
            {
                var meteo = (DataContext as Incidencia).Causa;

                if (meteo.ToUpper() == "LLUVIA") source = "Images/Lluvia.png";
                else if (meteo.ToUpper() == "NIEVE") source = "Images/Nieve.png";
                else source = "Images/Viento.png";
            }

            imgSenyal.Source =
                new BitmapImage(
                    new Uri(source, UriKind.Relative));


//            this.MapaLayer.Children.Clear();

//            this.Mapa.SetView(
//                new GeoCoordinate(
//                    e.Position.Location.Latitude,
//                    e.Position.Location.Longitude,
//                    e.Position.Location.Altitude)
//                    , 17
//                    , e.Position.Location.Course);

//            Mapa.BorderThickness = new Thickness(1, 1, 1, 1);
//            Mapa.BorderBrush = new SolidColorBrush(Colors.Black);

//            var pushpin = new Pushpin
//            {
//                Background = new SolidColorBrush(Colors.Black),
//                Content = "Yo"
//            };

//            this.MapaLayer
//                .AddChild(pushpin,
//                new GeoCoordinate(e.Position.Location.Latitude,
//                    e.Position.Location.Longitude, 0));

        }

        // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);
                DataContext = App.ViewModel.Incidencias[index];
            }
        }
    }
}